﻿using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using 基于WPF的电缆采样数据分析系统.Models.Enums;
using 基于WPF的电缆采样数据分析系统.Models.Task;
using 基于WPF的电缆采样数据分析系统.MsgEvents;
using 基于WPF的电缆采样数据分析系统.MsgEvents.AccountingReport;
using 基于WPF的电缆采样数据分析系统.Service;

namespace 基于WPF的电缆采样数据分析系统.ViewModels.TDRWork.MenuPage
{
    public class PassagePageUCViewModel : BindableBase
    {
        /// <summary>
        /// 对话服务
        /// </summary>
        private readonly DialogHostService DialogHostService;


        public PassagePageUCViewModel(DialogHostService _DialogHostService, IEventAggregator _Aggregator)
        {
            // 对话框
            DialogHostService = _DialogHostService;

            // 发布订阅
            Aggregator = _Aggregator;
            Aggregator.GetEvent<MsgEvent>().Subscribe(InitCurrentTaskParameter);
            Aggregator.GetEvent<MsgClearChannelInfoEvent>().Subscribe(ClearChannelInfo); 
            Aggregator.GetEvent<MsgSaveRefWavStatus>().Subscribe(UpdateRefWavStatus); 

            // 绑定X轴起点减少
            XASP_ReduceCmm = new DelegateCommand(XASP_ReduceFun);
            // 绑定X轴起点增加
            XASP_AddCmm = new DelegateCommand(XASP_AddFun);

            // 绑定X轴终点减少
            XAEP_ReduceCmm = new DelegateCommand(XAEP_ReduceFun);
            // 绑定X轴终点增加
            XAEP_AddCmm = new DelegateCommand(XAEP_AddFun);

            // 绑定X轴刻度递减
            XAS_ReduceCmm = new DelegateCommand(XAS_ReduceFun);
            // 绑定X轴刻度递增
            XAS_AddCmm = new DelegateCommand(XAS_AddFun);

            // 绑定Y轴起点减少
            YASP_ReduceCmm = new DelegateCommand(YASP_ReduceFun);
            // 绑定Y轴起点增加
            YASP_AddCmm = new DelegateCommand(YASP_AddFun);

            // 绑定Y轴终点减少
            YAEP_ReduceCmm = new DelegateCommand(YAEP_ReduceFun);
            // 绑定Y轴终点增加
            YAEP_AddCmm = new DelegateCommand(YAEP_AddFun);

            // 绑定Y轴刻度递减
            YAS_ReduceCmm = new DelegateCommand(YAS_ReduceFun);
            // 绑定Y轴刻度递增
            YAS_AddCmm = new DelegateCommand(YAS_AddFun);

            // 绑定波形向左平移
            XAT_ReduceCmm = new DelegateCommand(XAT_ReduceFun);
            // 绑定波形向右平移
            XAT_AddCmm = new DelegateCommand(XAT_AddFun);

            // 绑定波形向下平移
            YAT_ReduceCmm = new DelegateCommand(YAT_ReduceFun);
            // 绑定波形向上平移
            YAT_AddCmm = new DelegateCommand(YAT_AddFun);

            // 绑定切换通道命令
            ChannelTBtnCmm = new DelegateCommand<PassageType?>(ToggleChannel);

            // 绑定打开计算器
            OpenCalculatorCmm = new DelegateCommand<AxisParameterType?>(OpenCalculatorFun);
        }


        #region 通道类型
        /// <summary>
        /// 通道类型
        /// </summary>
        private PassageType _ChannelConfig;

        /// <summary>
        /// 通道类型
        /// </summary>
        public PassageType ChannelConfig
        {
            get { return _ChannelConfig; }
            set { 
                _ChannelConfig = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 是否选中通道一
        /// </summary>
        private bool _IsChannelOneChecked;

        /// <summary>
        /// 是否选中通道一
        /// </summary>
        public bool IsChannelOneChecked
        {
            get { return _IsChannelOneChecked; }
            set { 
                _IsChannelOneChecked = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 是否选中通道二
        /// </summary>
        private bool _IsChannelTwoChecked;

        /// <summary>
        /// 是否选中通道二
        /// </summary>
        public bool IsChannelTwoChecked
        {
            get { return _IsChannelTwoChecked; }
            set { 
                _IsChannelTwoChecked = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 是否选中通道三
        /// </summary>
        private bool _IsChannelThreeChecked;

        /// <summary>
        /// 是否选中通道三
        /// </summary>
        public bool IsChannelThreeChecked
        {
            get { return _IsChannelThreeChecked; }
            set { 
                _IsChannelThreeChecked = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 切换通道
        /// </summary>
        /// <param name="currentChannel"></param>
        private void ChannelChanged(PassageType currentChannel)
        {
            switch(currentChannel)
            {
                case PassageType.ChannelOne:
                    IsChannelOneChecked = true;
                    IsChannelTwoChecked = false;
                    IsChannelThreeChecked = false;
                    break;
                case PassageType.ChannelTwo:
                    IsChannelOneChecked = false;
                    IsChannelTwoChecked = true;
                    IsChannelThreeChecked = false;
                    break;
                case PassageType.ChannelThree:
                    IsChannelOneChecked = false;
                    IsChannelTwoChecked = false;
                    IsChannelThreeChecked = true;
                    break;
            }
        }

        #endregion


        #region 参考波形集合
        /// <summary>
        /// 参考波形集合
        /// </summary>
        private List<RefWavformType> _CurrentRefWavformTypeList;

        /// <summary>
        /// 参考波形集合
        /// </summary>
        public List<RefWavformType> CurrentRefWavformTypeList
        {
            get { return _CurrentRefWavformTypeList; }
            set { 
                _CurrentRefWavformTypeList = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// R1参考波形
        /// </summary>
        private bool _R1Check;

        /// <summary>
        /// R1参考波形
        /// </summary>
        public bool R1Check
        {
            get { return _R1Check; }
            set { 
                _R1Check = value;
                if (value)
                {
                    PassagePageRefWavChanged(RefWavformType.RefWavformOne, value);
                } else
                {
                    RealTimeOscilloscopeViewModel.Instance.HiddenRefWavform(RefWavformType.RefWavformOne);

                    // 更新参考波形信息
                    Aggregator.GetEvent<MsgUpdateReferenceWaveform>().Publish();
                }
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// R2参考波形
        /// </summary>
        private bool _R2Check;

        /// <summary>
        /// R2参考波形
        /// </summary>
        public bool R2Check
        {
            get { return _R2Check; }
            set
            {
                _R2Check = value;
                if (value)
                {
                    PassagePageRefWavChanged(RefWavformType.RefWavformTwo, value);
                }
                else
                {
                    RealTimeOscilloscopeViewModel.Instance.HiddenRefWavform(RefWavformType.RefWavformTwo);

                    // 更新参考波形信息
                    Aggregator.GetEvent<MsgUpdateReferenceWaveform>().Publish();
                }
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// R3参考波形
        /// </summary>
        private bool _R3Check;

        /// <summary>
        /// R3参考波形
        /// </summary>
        public bool R3Check
        {
            get { return _R3Check; }
            set
            {
                _R3Check = value;
                if (value)
                {
                    PassagePageRefWavChanged(RefWavformType.RefWavformThree, value);
                }
                else
                {
                    RealTimeOscilloscopeViewModel.Instance.HiddenRefWavform(RefWavformType.RefWavformThree);

                    // 更新参考波形信息
                    Aggregator.GetEvent<MsgUpdateReferenceWaveform>().Publish();
                }
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// R4参考波形
        /// </summary>
        private bool _R4Check;

        /// <summary>
        /// R4参考波形
        /// </summary>
        public bool R4Check
        {
            get { return _R4Check; }
            set
            {
                _R4Check = value;
                if (value)
                {
                    PassagePageRefWavChanged(RefWavformType.RefWavformFour, value);
                }
                else
                {
                    RealTimeOscilloscopeViewModel.Instance.HiddenRefWavform(RefWavformType.RefWavformFour);

                    // 更新参考波形信息
                    Aggregator.GetEvent<MsgUpdateReferenceWaveform>().Publish();
                }
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// R5参考波形
        /// </summary>
        private bool _R5Check;

        /// <summary>
        /// R5参考波形
        /// </summary>
        public bool R5Check
        {
            get { return _R5Check; }
            set
            {
                _R5Check = value;
                if (value)
                {
                    PassagePageRefWavChanged(RefWavformType.RefWavformFive, value);
                }
                else
                {
                    RealTimeOscilloscopeViewModel.Instance.HiddenRefWavform(RefWavformType.RefWavformFive);

                    // 更新参考波形信息
                    Aggregator.GetEvent<MsgUpdateReferenceWaveform>().Publish();
                }
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// R6参考波形
        /// </summary>
        private bool _R6Check;

        /// <summary>
        /// R6参考波形
        /// </summary>
        public bool R6Check
        {
            get { return _R6Check; }
            set
            {
                _R6Check = value;
                if (value)
                {
                    PassagePageRefWavChanged(RefWavformType.RefWavformSix, value);
                }
                else
                {
                    RealTimeOscilloscopeViewModel.Instance.HiddenRefWavform(RefWavformType.RefWavformSix);

                    // 更新参考波形信息
                    Aggregator.GetEvent<MsgUpdateReferenceWaveform>().Publish();
                }
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// R7参考波形
        /// </summary>
        private bool _R7Check;

        /// <summary>
        /// R7参考波形
        /// </summary>
        public bool R7Check
        {
            get { return _R7Check; }
            set
            {
                _R7Check = value;
                if (value)
                {
                    PassagePageRefWavChanged(RefWavformType.RefWavformSeven, value);
                }
                else
                {
                    RealTimeOscilloscopeViewModel.Instance.HiddenRefWavform(RefWavformType.RefWavformSeven);

                    // 更新参考波形信息
                    Aggregator.GetEvent<MsgUpdateReferenceWaveform>().Publish();
                }
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// R8参考波形
        /// </summary>
        private bool _R8Check;

        /// <summary>
        /// R8参考波形
        /// </summary>
        public bool R8Check
        {
            get { return _R8Check; }
            set
            {
                _R8Check = value;
                if (value)
                {
                    PassagePageRefWavChanged(RefWavformType.RefWavformEight, value);
                }
                else
                {
                    RealTimeOscilloscopeViewModel.Instance.HiddenRefWavform(RefWavformType.RefWavformEight);

                    // 更新参考波形信息
                    Aggregator.GetEvent<MsgUpdateReferenceWaveform>().Publish();
                }
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// 更新参考波形状态
        /// </summary>
        private void UpdateRefWavStatus()
        {
            R1Check = GlobalVariableEdit.R1Display;
            R2Check = GlobalVariableEdit.R2Display;
            R3Check = GlobalVariableEdit.R3Display;
            R4Check = GlobalVariableEdit.R4Display;
            R5Check = GlobalVariableEdit.R5Display;
            R6Check = GlobalVariableEdit.R6Display;
            R7Check = GlobalVariableEdit.R7Display;
            R8Check = GlobalVariableEdit.R8Display;
        }


        /// <summary>
        /// 通道参考波形更新
        /// </summary>
        /// <param name="R"></param>
        /// <param name="IsChecked"></param>
        private async void PassagePageRefWavChanged(RefWavformType R, bool IsChecked)
        {
            switch (R)
            {
                case RefWavformType.RefWavformOne:
                    GlobalVariableEdit.R1Display = IsChecked;
                    if (IsChecked)
                    {
                        await RealTimeOscilloscopeViewModel.Instance.ByTypeDisplayRefWavform(RefWavformType.RefWavformOne);
                    }
                    break;
                case RefWavformType.RefWavformTwo:
                    GlobalVariableEdit.R2Display = IsChecked;
                    if (IsChecked)
                    {
                        await RealTimeOscilloscopeViewModel.Instance.ByTypeDisplayRefWavform(RefWavformType.RefWavformTwo);
                    }
                    break;
                case RefWavformType.RefWavformThree:
                    GlobalVariableEdit.R3Display = IsChecked;
                    if (IsChecked)
                    {
                        await RealTimeOscilloscopeViewModel.Instance.ByTypeDisplayRefWavform(RefWavformType.RefWavformThree);
                    }
                    break;
                case RefWavformType.RefWavformFour:
                    GlobalVariableEdit.R4Display = IsChecked;
                    if (IsChecked)
                    {
                        await RealTimeOscilloscopeViewModel.Instance.ByTypeDisplayRefWavform(RefWavformType.RefWavformFour);
                    }
                    break;
                case RefWavformType.RefWavformFive:
                    GlobalVariableEdit.R5Display = IsChecked;
                    if (IsChecked)
                    {
                        await RealTimeOscilloscopeViewModel.Instance.ByTypeDisplayRefWavform(RefWavformType.RefWavformFive);
                    }
                    break;
                case RefWavformType.RefWavformSix:
                    GlobalVariableEdit.R6Display = IsChecked;
                    if (IsChecked)
                    {
                        await RealTimeOscilloscopeViewModel.Instance.ByTypeDisplayRefWavform(RefWavformType.RefWavformSix);
                    }
                    break;
                case RefWavformType.RefWavformSeven:
                    GlobalVariableEdit.R7Display = IsChecked;
                    if (IsChecked)
                    {
                        await RealTimeOscilloscopeViewModel.Instance.ByTypeDisplayRefWavform(RefWavformType.RefWavformSeven);
                    }
                    break;
                case RefWavformType.RefWavformEight:
                    GlobalVariableEdit.R8Display = IsChecked;
                    if (IsChecked)
                    {
                        await RealTimeOscilloscopeViewModel.Instance.ByTypeDisplayRefWavform(RefWavformType.RefWavformEight);
                    }
                    break;
            }

            // 更新参考波形信息
            Aggregator.GetEvent<MsgUpdateReferenceWaveform>().Publish();
        }

        #endregion


        #region 水平参数
        /// <summary>
        /// X轴起点
        /// </summary>
        private float _XAxisStartPoint;

        /// <summary>
        /// X轴起点
        /// </summary>
        public float XAxisStartPoint
        {
            get { return _XAxisStartPoint; }
            set { 
                _XAxisStartPoint = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// X轴终点
        /// </summary>
        private float _XAxisEndPoint;

        /// <summary>
        /// X轴终点
        /// </summary>
        public float XAxisEndPoint
        {
            get { return _XAxisEndPoint; }
            set { 
                _XAxisEndPoint = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// X轴刻度
        /// </summary>
        private float _XAxisScale;

        /// <summary>
        /// X轴刻度
        /// </summary>
        public float XAxisScale
        {
            get { return _XAxisScale; }
            set { 
                _XAxisScale = value;
                RaisePropertyChanged();
            }
        }

        #endregion


        #region 垂直参数
        /// <summary>
        /// Y轴起点
        /// </summary>
        private float _YAxisStartPoint;

        /// <summary>
        /// Y轴起点
        /// </summary>
        public float YAxisStartPoint
        {
            get { return _YAxisStartPoint; }
            set { 
                _YAxisStartPoint = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Y轴终点
        /// </summary>
        private float _YAxisEndPoint;

        /// <summary>
        /// Y轴终点
        /// </summary>
        public float YAxisEndPoint
        {
            get { return _YAxisEndPoint; }
            set { 
                _YAxisEndPoint = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Y轴刻度
        /// </summary>
        private float _YAxisScale;

        /// <summary>
        /// Y轴刻度
        /// </summary>
        public float YAxisScale
        {
            get { return _YAxisScale; }
            set { 
                _YAxisScale = value;
                RaisePropertyChanged();
            }
        }

        #endregion


        #region 偏移参数
        /// <summary>
        /// X轴平移
        /// </summary>
        private float _XAxisTranslation;

        /// <summary>
        /// X轴平移
        /// </summary>
        public float XAxisTranslation
        {
            get { return _XAxisTranslation; }
            set { 
                _XAxisTranslation = value;
                RaisePropertyChanged();
            }
        }

        /// <summary>
        /// Y轴平移
        /// </summary>
        private float _YAxisTranslation;

        /// <summary>
        /// Y轴平移
        /// </summary>
        public float YAxisTranslation
        {
            get { return _YAxisTranslation; }
            set { 
                _YAxisTranslation = value;
                RaisePropertyChanged();
            }
        }

        #endregion


        #region 任务信息
        /// <summary>
        /// 当前任务ID
        /// </summary>
        private int _CurrentTaskId;

        /// <summary>
        /// 当前任务ID
        /// </summary>
        public int CurrentTaskId
        {
            get { return _CurrentTaskId; }
            set { 
                _CurrentTaskId = value;
                RaisePropertyChanged();
            }
        }


        #endregion


        #region X轴起点命令
        /// <summary>
        /// X轴起点减少
        /// </summary>
        public DelegateCommand XASP_ReduceCmm { get; set; }

        /// <summary>
        /// X轴起点减少
        /// </summary>
        private void XASP_ReduceFun()
        {
            XAxisStartPoint -= 10;

            // 更新当前任务
            UpdateTask();
        }


        /// <summary>
        /// X轴起点增加
        /// </summary>
        public DelegateCommand XASP_AddCmm { get; set; }

        /// <summary>
        /// X轴起点增加
        /// </summary>
        private void XASP_AddFun()
        {
            XAxisStartPoint += 10;

            // 更新当前任务
            UpdateTask();
        }
        #endregion


        #region X轴终点命令
        /// <summary>
        /// X轴终点减少
        /// </summary>
        public DelegateCommand XAEP_ReduceCmm { get; set; }

        /// <summary>
        /// X轴终点减少
        /// </summary>
        private void XAEP_ReduceFun()
        {
            XAxisEndPoint -= 10;

            // 更新当前任务
            UpdateTask();
        }


        /// <summary>
        /// X轴终点增加
        /// </summary>
        public DelegateCommand XAEP_AddCmm { get; set; }

        /// <summary>
        /// X轴终点增加
        /// </summary>
        private void XAEP_AddFun()
        {
            XAxisEndPoint += 10;

            // 更新当前任务
            UpdateTask();
        }

        #endregion


        #region X轴刻度命令
        /// <summary>
        /// X轴刻度递减
        /// </summary>
        public DelegateCommand XAS_ReduceCmm { get; set; }

        /// <summary>
        /// X轴刻度递减
        /// </summary>
        private void XAS_ReduceFun()
        {
            XAxisScale /= 2;

            // 更新当前任务
            UpdateTask();
        }


        /// <summary>
        /// X轴刻度递增
        /// </summary>
        public DelegateCommand XAS_AddCmm { get; set; }

        /// <summary>
        /// X轴刻度递增
        /// </summary>
        private void XAS_AddFun()
        {
            XAxisScale *= 2;

            // 更新当前任务
            UpdateTask();
        }

        #endregion


        #region X轴平移命令
        /// <summary>
        /// 波形向左平移
        /// </summary>
        public DelegateCommand XAT_ReduceCmm { get; set; }

        /// <summary>
        /// 波形向左平移
        /// </summary>
        private void XAT_ReduceFun()
        {
            XAxisTranslation -= 10;

            // 更新当前任务
            UpdateTask();
        }

        /// <summary>
        /// 波形向右平移
        /// </summary>
        public DelegateCommand XAT_AddCmm { get; set; }

        /// <summary>
        /// 波形向右平移
        /// </summary>
        private void XAT_AddFun()
        {
            XAxisTranslation += 10;

            // 更新当前任务
            UpdateTask();
        }

        #endregion


        #region Y轴起点命令
        /// <summary>
        /// Y轴起点减少
        /// </summary>
        public DelegateCommand YASP_ReduceCmm { get; set; }

        /// <summary>
        /// Y轴起点减少
        /// </summary>
        private void YASP_ReduceFun()
        {
            YAxisStartPoint -= 10;

            // 更新当前任务
            UpdateTask();
        }


        /// <summary>
        /// Y轴起点增加
        /// </summary>
        public DelegateCommand YASP_AddCmm { get; set; }

        /// <summary>
        /// Y轴起点增加
        /// </summary>
        private void YASP_AddFun()
        {
            YAxisStartPoint += 10;

            // 更新当前任务
            UpdateTask();
        }

        #endregion


        #region Y轴终点命令
        /// <summary>
        /// Y轴终点减少
        /// </summary>
        public DelegateCommand YAEP_ReduceCmm { get; set; }

        /// <summary>
        /// Y轴终点减少
        /// </summary>
        private void YAEP_ReduceFun()
        {
            YAxisEndPoint -= 10;

            // 更新当前任务
            UpdateTask();
        }


        /// <summary>
        /// Y轴终点增加
        /// </summary>
        public DelegateCommand YAEP_AddCmm { get; set; }

        /// <summary>
        /// Y轴终点增加
        /// </summary>
        private void YAEP_AddFun()
        {
            YAxisEndPoint += 10;

            // 更新当前任务
            UpdateTask();
        }

        #endregion


        #region Y轴刻度命令
        /// <summary>
        ///Y轴刻度递减
        /// </summary>
        public DelegateCommand YAS_ReduceCmm { get; set; }

        /// <summary>
        /// Y轴刻度递减
        /// </summary>
        private void YAS_ReduceFun()
        {
            YAxisScale /= 2;

            // 更新当前任务
            UpdateTask();
        }


        /// <summary>
        /// Y轴刻度递增
        /// </summary>
        public DelegateCommand YAS_AddCmm { get; set; }

        /// <summary>
        /// Y轴刻度递增
        /// </summary>
        private void YAS_AddFun()
        {
            YAxisScale *= 2;

            // 更新当前任务
            UpdateTask();
        }

        #endregion


        #region Y轴平移命令

        /// <summary>
        /// 波形向下平移
        /// </summary>
        public DelegateCommand YAT_ReduceCmm { get; set; }

        /// <summary>
        /// 波形向下平移
        /// </summary>
        private void YAT_ReduceFun()
        {
            YAxisTranslation -= 10;

            // 更新当前任务
            UpdateTask();
        }

        /// <summary>
        /// 波形向上平移
        /// </summary>
        public DelegateCommand YAT_AddCmm { get; set; }

        /// <summary>
        /// 波形向上平移
        /// </summary>
        private void YAT_AddFun()
        {
            YAxisTranslation += 10;

            // 更新当前任务
            UpdateTask();
        }

        #endregion


        #region 打开计算器
        /// <summary>
        /// 打开计算器命令
        /// </summary>
        public DelegateCommand<AxisParameterType?> OpenCalculatorCmm { get; set; }

        /// <summary>
        /// 打开计算器方法
        /// </summary>
        private void OpenCalculatorFun(AxisParameterType? axisParameterType)
        {
            switch(axisParameterType)
            {
                case AxisParameterType.XAxisStartPoint:
                    CalculatorFun(XAxisStartPoint, "ns", axisParameterType);
                    break;
                case AxisParameterType.XAxisEndPoint:
                    CalculatorFun(XAxisEndPoint, "ns", axisParameterType);
                    break;
                case AxisParameterType.XAxisScale:
                    CalculatorFun(XAxisScale, "ns", axisParameterType);
                    break;
                case AxisParameterType.YAxisStartPoint:
                    CalculatorFun(YAxisStartPoint, "mV", axisParameterType);
                    break;
                case AxisParameterType.YAxisEndPoint:
                    CalculatorFun(YAxisEndPoint, "mV", axisParameterType);
                    break;
                case AxisParameterType.YAxisScale:
                    CalculatorFun(YAxisScale, "mV", axisParameterType);
                    break;
                case AxisParameterType.XAxisTranslation:
                    CalculatorFun(XAxisTranslation, "ns", axisParameterType);
                    break;
                case AxisParameterType.YAxisTranslation:
                    CalculatorFun(YAxisTranslation, "mV", axisParameterType);
                    break;
            }
        }


        /// <summary>
        /// 计算器内容设置
        /// </summary>
        /// <param name="value"></param>
        /// <param name="unit"></param>
        /// <param name="axisParameterType"></param>
        public async void CalculatorFun(float value, string unit, AxisParameterType? axisParameterType)
        {
            DialogParameters paras = new DialogParameters();
            paras.Add("ComputeValue", value);
            paras.Add("UnitValue", unit);

            var result = await DialogHostService.ShowDialog("CalculatorUC", paras);

            if (result.Result == ButtonResult.OK)
            {
                if (result.Parameters.ContainsKey("CurrentCalculatorValue"))
                {
                    ByAxisParameterTypeChanged(result.Parameters.GetValue<string>("CurrentCalculatorValue"), axisParameterType);

                    // 更新当前任务
                    UpdateTask();
                }
            }
        }

        /// <summary>
        /// 根据轴类型改变
        /// </summary>
        public void ByAxisParameterTypeChanged(string ComputeValue, AxisParameterType? axisParameterType)
        {
            bool result = float.TryParse(ComputeValue, out float number);

            if (result)
            {
                switch(axisParameterType)
                {
                    case AxisParameterType.XAxisStartPoint:
                        XAxisStartPoint = number;
                        break;
                    case AxisParameterType.XAxisEndPoint:
                        XAxisEndPoint = number;
                        break;
                    case AxisParameterType.XAxisScale:
                        XAxisScale = number;
                        break;
                    case AxisParameterType.YAxisStartPoint:
                        YAxisStartPoint = number;
                        break;
                    case AxisParameterType.YAxisEndPoint:
                        YAxisEndPoint = number;
                        break;
                    case AxisParameterType.YAxisScale:
                        YAxisScale = number;
                        break;
                    case AxisParameterType.XAxisTranslation:
                        XAxisTranslation = number;
                        break;
                    case AxisParameterType.YAxisTranslation:
                        YAxisTranslation = number;
                        break;
                }
            }
        }
        #endregion


        #region 发布事件-初始化当前任务参数-同步任务

        // 发布订阅
        private readonly IEventAggregator Aggregator;

        /// <summary>
        /// 初始化当前任务参数
        /// </summary>
        /// <param name="taskDataInfo"></param>
        public void InitCurrentTaskParameter(TaskDataInfo taskDataInfo)
        {
            CurrentTaskId = taskDataInfo.Tid;
            ChannelConfig = taskDataInfo.ChannelConfig;
            XAxisStartPoint = taskDataInfo.HorizontalArgument.AxisOrigin;
            XAxisEndPoint = taskDataInfo.HorizontalArgument.AxisEndPoint;
            XAxisScale = taskDataInfo.HorizontalArgument.AxisScale;
            YAxisStartPoint = taskDataInfo.VerticalArgument.AxisOrigin;
            YAxisEndPoint = taskDataInfo.VerticalArgument.AxisEndPoint;
            YAxisScale = taskDataInfo.VerticalArgument.AxisScale;
            XAxisTranslation = taskDataInfo.XAxisTranslation;
            YAxisTranslation = taskDataInfo.YAxisTranslation;

            ChannelChanged(ChannelConfig);
        }

        /// <summary>
        /// 修改当前任务
        /// </summary>
        public void UpdateCurrentTask()
        {
            var taskDataInfo = new TaskDataInfo();
            taskDataInfo.HorizontalArgument = new ChannelArgument();
            taskDataInfo.VerticalArgument = new ChannelArgument();

            taskDataInfo.ChannelConfig = ChannelConfig;
            taskDataInfo.HorizontalArgument.AxisOrigin = XAxisStartPoint;
            taskDataInfo.HorizontalArgument.AxisEndPoint = XAxisEndPoint;
            taskDataInfo.HorizontalArgument.AxisScale = XAxisScale;
            taskDataInfo.VerticalArgument.AxisOrigin = YAxisStartPoint;
            taskDataInfo.VerticalArgument.AxisEndPoint = YAxisEndPoint;
            taskDataInfo.VerticalArgument.AxisScale = YAxisScale;
            taskDataInfo.XAxisTranslation = XAxisTranslation;
            taskDataInfo.YAxisTranslation = YAxisTranslation;
            Aggregator.GetEvent<MsgEvent>().Publish(taskDataInfo);
        }

        /// <summary>
        /// 更新当前任务
        /// </summary>
        public void UpdateTask()
        {
            var Tid = CurrentTaskId;

            // 更新当前任务
            UpdateCurrentTask();

            CurrentTaskId = Tid;
        }
        #endregion


        #region 切换通道

        /// <summary>
        /// 切换通道命令
        /// </summary>
        public DelegateCommand<PassageType?> ChannelTBtnCmm { get; set; }

        /// <summary>
        /// 切换通道
        /// </summary>
        /// <param name="passageType"></param>
        public void ToggleChannel(PassageType? passageType)
        {
            // 通道
            ChannelConfig = (PassageType)passageType;

            ChannelChanged((PassageType)passageType);

            // 更新当前任务
            UpdateTask();
        }

        #endregion


        #region 清空通道信息
        private void ClearChannelInfo()
        {
            CurrentTaskId = 0;
            ChannelConfig = PassageType.ChannelOne;
            XAxisStartPoint = 0;
            XAxisEndPoint = 0;
            XAxisScale = 0;
            YAxisStartPoint = 0;
            YAxisEndPoint = 0;
            YAxisScale = 0;
            XAxisTranslation = 0;
            YAxisTranslation = 0;

            IsChannelOneChecked = false;
            IsChannelTwoChecked = false;
            IsChannelThreeChecked = false;
        }

        #endregion
    }
}
